import pymysql
from flask import Blueprint, render_template, request, redirect, url_for

bp = Blueprint('register', __name__, url_prefix='/register')


@bp.route('/people', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        return render_template('register.html')
    else:
        data = request.form
        db = pymysql.connect(host='localhost', user="root", passwd='123456', database='final_assignment')  # 打开数据库连接
        cursor = db.cursor()  # 使用cursor()方法获取操作游标
        sql = 'insert into borrower_information(账号,密码) values("%s", "%s")'  # SQL 插入语句
        try:
            cursor.execute(sql % (data['loginId'], data['password']))  # 执行sql语句
            db.commit()
        except:
            db.rollback()  # 发生错误时回滚
        db.close()  # 关闭数据库连接
        return redirect(url_for('user.index'))
